package HDFS;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.fs.permission.FsPermission;
import org.apache.hadoop.util.Progressable;
import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * @author caoxiaofeng
 * @date 2021-07-11 11:51
 */
public class HDFSClient {

    public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException {
        // 1. hdfs 配置
        Configuration conf = new Configuration();
        //conf.set("fs.defaultFS","hdfs://node201:9000");

        // 2. 获取hdfs客户端对象
        //FileSystem fs = FileSystem.get(conf);

        FileSystem fs = FileSystem.get(new URI("hdfs://node201:9000"),conf,"root");

        //3. 在hdfs上创建路径
        fs.mkdirs(new Path("/0529/dashen/banzhang"));

        // 关闭资源
        fs.close();

        System.out.println("over");
    }


    // 文件上传
    @Test
    public void testCopyFromLocalFile() throws URISyntaxException, IOException, InterruptedException {
        Configuration configuration = new Configuration();
        configuration.set("dfs.replication","2");
        // 获取hdfs 客户端对象
        FileSystem fs = FileSystem.get(new URI("hdfs://node201:9000"),configuration,"root");
        // 拷贝文件到hdfs
        fs.copyFromLocalFile(new Path("E:\\Java并发编程的艺术.pdf"),new Path("/Java并发编程的艺术3.pdf"));

        fs.close();
        System.out.println("over");
    }


    // 文件下载
    @Test
    public void testCopyToLocalFile() throws URISyntaxException, IOException, InterruptedException {
        Configuration configuration = new Configuration();
        // 获取hdfs 客户端对象
        FileSystem fs = FileSystem.get(new URI("hdfs://node201:9000"),configuration,"root");

        // 下载文件到本地
        fs.copyToLocalFile(new Path("/Java并发编程的艺术3.pdf"),new Path("E:\\Java并发编程的艺术bak.pdf"));

        fs.close();
        System.out.println("over");
    }


    // 文件删除
    @Test
    public void testDelete() throws URISyntaxException, IOException, InterruptedException {
        Configuration configuration = new Configuration();
        // 获取hdfs 客户端对象
        FileSystem fs = FileSystem.get(new URI("hdfs://node201:9000"),configuration,"root");

        // 下载文件到本地
        fs.delete(new Path("/0529"),true);

        fs.close();
        System.out.println("over");
    }

    // 文件重命名
    @Test
    public void testRename() throws URISyntaxException, IOException, InterruptedException {
        Configuration configuration = new Configuration();
        // 获取hdfs 客户端对象
        FileSystem fs = FileSystem.get(new URI("hdfs://node201:9000"),configuration,"root");

        // 下载文件到本地
        fs.rename(new Path("/Java并发编程的艺术2.pdf"),new Path("/Java并发编程的艺术4.pdf"));

        fs.close();
        System.out.println("over");
    }

    // 遍历文件 获取文件信息
    @Test
    public void testListFile() throws URISyntaxException, IOException, InterruptedException {
        Configuration configuration = new Configuration();
        // 获取hdfs 客户端对象
        FileSystem fs = FileSystem.get(new URI("hdfs://node201:9000"),configuration,"root");

        // 获取文件迭代器
        RemoteIterator<LocatedFileStatus> fileIterator = fs.listFiles(new Path("/"), true);

        // 迭代文件
        while(fileIterator.hasNext()){
            LocatedFileStatus fileStatus = fileIterator.next();

            System.out.println(fileStatus.getPath().getName()); //文件名称
            System.out.println(fileStatus.getPermission()); //文件权限
            System.out.println(fileStatus.getLen()); //文件长度

            // 文件存储块
            BlockLocation[] blockLocations = fileStatus.getBlockLocations();

            for (BlockLocation blockLocation : blockLocations){
                String[] hosts = blockLocation.getHosts();
                for (String host : hosts){
                    System.out.println(host);
                }
            }

            System.out.println("--------------");
        }

        fs.close();
        System.out.println("over");
    }

    // 判断是文件还是文件夹
    @Test
    public void testListStatus() throws URISyntaxException, IOException, InterruptedException {
        Configuration configuration = new Configuration();
        // 获取hdfs 客户端对象
        FileSystem fs = FileSystem.get(new URI("hdfs://node201:9000"),configuration,"root");

        // 获取文件迭代器
        RemoteIterator<LocatedFileStatus> fileIterator = fs.listFiles(new Path("/"), true);

        // 判断操作
        FileStatus[] fileStatuses = fs.listStatus(new Path("/"));

        for (FileStatus fileStatus : fileStatuses){
            if (fileStatus.isFile()){
                System.out.println("file:"+fileStatus.getPath().getName());
            }else {
                System.out.println("Dir:"+fileStatus.getPath().getName());
            }
        }

        // 关闭流
        fs.close();
        System.out.println("over");
    }
}
